package com.example.administrator.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

/**
 * Created by Administrator on 2016/11/25.
 */
public class HttpUtils {


    public static byte[] getBytes(String params) {

        InputStream is=null;
        ByteArrayOutputStream baos=null;
        try {
            String encode = URLEncoder.encode("%", "UTF-8");
            params=params+"s";
            URL url=new URL(params);


            HttpURLConnection con= (HttpURLConnection) url.openConnection();
            con.setConnectTimeout(5000);
            if(con.getResponseCode()==200){
                is=con.getInputStream();
                baos=new ByteArrayOutputStream();

                int len=0;
                byte[] buf=new byte[1024*8];
                while ((len=is.read(buf))!=-1){

                    baos.write(buf,0,len);
                }
                return baos.toByteArray();
            }


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }
}
